import { PlainObjectType } from "./PlainType";

// 交集
type Intersection<A, B> = A extends B ? A : never;

// 差集
type Difference<A, B> = A extends B ? never : A;

// 补集
type Complement<A, B extends A> = Difference<A, B>;

// 属性名交集
type ObjectKeysIntersection<
	T extends PlainObjectType,
	U extends PlainObjectType
> = Intersection<keyof T, keyof U>;

// 属性名差集
type ObjectKeysDifference<
	T extends PlainObjectType,
	U extends PlainObjectType
> = Difference<keyof T, keyof U>;

// 属性名补集
type ObjectKeysComplement<T extends U, U extends PlainObjectType> = Complement<
	keyof T,
	keyof U
>;
//对象的交集, T的优先级更高
export type ObjectIntersection<
	T extends PlainObjectType,
	U extends PlainObjectType
> = Pick<T, ObjectKeysIntersection<T, U>>;

//对象的差集
export type ObjectDifference<
	T extends PlainObjectType,
	U extends PlainObjectType
> = Pick<T, ObjectKeysDifference<T, U>>;

//对象的补集
export type ObjectComplement<T extends U, U extends PlainObjectType> = Pick<
	T,
	ObjectKeysComplement<T, U>
>;
//对象的并集, 以T的优先级更高
export type ObjectMerge<
	T extends PlainObjectType,
	U extends PlainObjectType
	// T 比 U 多的部分，加上 T 与 U 交集的部分(类型不同则以 T 优先级更高，再加上 U 比 T 多的部分即可
> = ObjectDifference<T, U> & ObjectIntersection<T, U> & ObjectDifference<U, T>;
